(function($){
	function filterHtml(html) {
		return html.toString().replace(/'/g, "&#39;")
					.replace(/"/g, "&quot;")
					.replace(/</g, "&lt;")
					.replace(/>/g, "&gt;");
	}
	
	$.filterHtml = filterHtml;
	$.getByteLength = function(str) {
		return str.replace(/[^\x00-\xff]/g, "aa").length;
	}
	
	function getAreaHeight() { return document.documentElement.clientHeight; };
	function getAreaWidth() { return document.documentElement.clientWidth; };
	
	var cLayer = null;
	var cCallback = null;
	var cBtns = [];
	
	function link_click() {
		if (cBtns.length) {
			if (cLayer) {
				cLayer.hide();
			}
			
			$.each(cBtns, function(k, v) {
				$("#" + v).unbind("click", link_click);
			});
		}
		
		cLayer.hide();
		if (cCallback) cCallback.call(this);
		cCallback = null;
	}
	
	$.fn.showMiddleDialog = function(btns, callback) {
		var layer = this;
		
		if (cBtns.length) {
			
			$.each(cBtns, function(k, v) {
				$("#" + v).unbind("click", link_click);
			});
		}
		
		this.css({
			"position": "absolute",
			"z-index": "2010"
		});
		
		callback = callback || function(){};
		
		cLayer = layer;
		cCallback = callback;
		
		this.show();
		if(window.parent){
			//若是在框架里(还得减去框架顶部79象素高，以便居于屏幕中间)
			var t=$(window.parent).scrollTop() + (window.parent.document.documentElement.clientHeight - this.height()) / 2 - 79;
		}
		else{
			var t=$(document).scrollTop() + (getAreaHeight() - this.height()) / 2;
		}
		this.css({
			"left": $(document).scrollLeft() + (getAreaWidth() - this.width()) / 2,
			"top": t 
		});

		if(t+this.height() > getAreaHeight()){
			//若层所在的位置被框架档住了,那么重置本框架的高度为t+this.height()+100(一定余量)
			if(window.parent){
			    parent.setFrameHeight((t+this.height()));
			}
		}

		
		if (btns) {
			$.each(btns, function(key, val) {
				$("#" + val).bind("click", link_click);
			});
			
			cBtns = btns;
		}
	}
})(jQuery);
// 联动菜单
(function($){
	function createChildSelector(selector, json) {
		var defVal = selector.attr("defVal") || 0;
		var html = [];

		if (json) {
			$.each(json, function(key, val) {
				html.push("<option value=\"" + $.filterHtml(val.val) + "\">" + $.filterHtml(val.name) + "</option>");
				
				if (null != defVal && defVal == val.val) {
					cIdx = key;
				}
			});
		} else {
			return;
		}
		
		
		selector.html(html.join(""));
		
		if (selector.attr("childSelector")) {
			var childSelector = $("#" + selector.attr("childSelector"));
			json[cIdx].items = json[cIdx].items || [];
			createChildSelector(childSelector, json[cIdx].items);
			
			selector.bind("change", function(){
				if (json[selector.attr("selectedIndex")] == null) return;
				json[selector.attr("selectedIndex")].items = json[selector.attr("selectedIndex")].items || [];
				createChildSelector(childSelector, json[selector.attr("selectedIndex")].items);
			});
		}
	}
	
	// 下拉菜单二级联动
	$.fn.selectorInit = function() {
		var selector = $(this);
		
		createChildSelector(selector, window[selector.attr("selectorData")]);
		
		var elm = selector;
		var options = [];

		while(true) {
			if (elm.attr("defVal") != null) {
				var items = elm.children("option[value='" + elm.attr("defVal") + "']");
				if (items.size()) {
					options.push(items[0]);
				}
			}
			
			if (elm.attr("childSelector") != null) {
				elm = $("#" + elm.attr("childSelector"));
			} else {
				break;
			}
		}
		setTimeout(function() {
			$.each(options, function(k, v) {
				v.selected = true;
			});
		}, 200);
	}
})(jQuery);

